package pATT.viewer;

import java.awt.Dimension;
import java.awt.Toolkit;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;

import pATT.core.Attribute;
import pATT.core.Instance;
import pATT.core.InstanceManager;
import pATT.logger.Logger;
import pATT.logger.Message;

@SuppressWarnings("serial")
public class EditorView extends JDialog {

	private JPanel jContentPane = null;
	private JScrollPane jScrollPane = null;
	private JButton jButton = null;
	private JButton jButton1 = null;
	private JLabel jLabel = null;
	private JLabel jLabel1 = null;
	private JTable jTable = null;

	/**
	 * This is the default constructor
	 */
	public EditorView() {
		super();
		initialize();
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setSize(800, 476);
		this.setLocation(new java.awt.Point(0,0));
		this.setTitle("Viewer");
		this.setModal(true);
		Dimension pantalla = Toolkit.getDefaultToolkit().getScreenSize();
		Dimension freim = this.getSize();
		if (pantalla.width < freim.width){
			freim.width = pantalla.width;
		}
		if (pantalla.height < freim.height) {
			freim.height = pantalla.height;
		}
		this.setLocation((pantalla.width-freim.width)/2,(pantalla.height-freim.height)/2);
		this.setMinimumSize(new java.awt.Dimension(288,224));
		this.setContentPane(getJContentPane());
		this.repaint();
	}

	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			jLabel = new JLabel();
			jLabel.setText(InstanceManager.eINSTANCE.nameRelation());
			jLabel.setBounds(new java.awt.Rectangle(143,10,623,20));
			jLabel1 = new JLabel();
			jLabel1.setFont(new java.awt.Font("Microsoft Sans Serif", java.awt.Font.PLAIN, 11));
			jLabel1.setHorizontalTextPosition(javax.swing.SwingConstants.RIGHT);
			jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
			jLabel1.setPreferredSize(new java.awt.Dimension(73,20));
			jLabel1.setLocation(new java.awt.Point(50,10));
			jLabel1.setSize(new java.awt.Dimension(91,20));
			jLabel1.setText("Relation Name:");
			jContentPane = new JPanel();
			jContentPane.setLayout(null);
			jContentPane.setPreferredSize(new java.awt.Dimension(800,600));
			jContentPane.setBackground(java.awt.SystemColor.control);
			//jContentPane1.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
			jContentPane.setBorder(javax.swing.BorderFactory.createCompoundBorder(javax.swing.BorderFactory.createLineBorder(java.awt.Color.gray,2), javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED)));
			
			jContentPane.add(getJScrollPane(), null);
			jContentPane.add(getJButton(), null);
			jContentPane.add(getJButton1(), null);
			jContentPane.add(jLabel, null);
			jContentPane.add(jLabel1, null);
		}
		return jContentPane;
	}
    /**
	 * This method initializes jScrollPane	
	 * 	
	 * @return javax.swing.JScrollPane	
	 */
	private JScrollPane getJScrollPane() {
		if (jScrollPane == null) {
			jScrollPane = new JScrollPane();
			jScrollPane.setPreferredSize(new java.awt.Dimension(280,50));
			jScrollPane.setBounds(new java.awt.Rectangle(6,36,778,375));
			jScrollPane.setViewportView(getJTable());
			jScrollPane.setBackground(java.awt.SystemColor.control);
		}
		return jScrollPane;
	}
	
	/**
	 * This method initializes jTable1	
	 * 	
	 * @return javax.swing.JTable	
	 */
	@SuppressWarnings("unchecked")
	private JTable getJTable() {
		if (jTable == null) {
			jTable = new JTable();
			jTable.setCellSelectionEnabled(false);
			DefaultTableModel model = (DefaultTableModel)jTable.getModel();
			Vector<Attribute> attributes=new Vector<Attribute>();
			model.addColumn("No");
			//Pongo los nombres de los atributos en la tabla
			attributes.addAll(InstanceManager.eINSTANCE.getAttributes());
			for (int i = 0; i < attributes.size(); i++) {
				model.addColumn(attributes.elementAt(i).name());
			}
			//Pongo las instancias en la tabla
			int numInstances=InstanceManager.eINSTANCE.numInstances();
			int cont=1;
			for (int j = 0; j < numInstances; j++) {
				Instance instance=InstanceManager.eINSTANCE.getInstance(j);
				Vector vec=new Vector();
				vec.add(cont);
				for (int k = 0; k < instance.numValues(); k++) {
					vec.add(instance.stringValue(k));
				}	
		     	model.addRow(vec.toArray());
		     	cont++;
			}
			//Pongo los combo con los valores que pueden tomar
			
			for (int e = 1; e < attributes.size()+1; e++) {
				TableColumn col = jTable.getColumnModel().getColumn(e);
				col.setCellEditor(new MyComboBoxEditor(InstanceManager.eINSTANCE.getVectorValuesAttribute(attributes.elementAt(e-1)).toArray()));
				
				
			}
		
		}
		return jTable;
	}


	/**
	 * This method initializes jButton	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButton() {
		if (jButton == null) {
			jButton = new JButton();
			jButton.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
			jButton.setFont(new java.awt.Font("Microsoft Sans Serif", java.awt.Font.PLAIN, 11));
			jButton.setBounds(new java.awt.Rectangle(278,418,93,22));
			jButton.setBackground(java.awt.SystemColor.control);
			jButton.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
			jButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
			jButton.setText("Ok");
			jButton.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					getEditorValues();
					repaint();
					dispose();
					
				}
			});
		}
		return jButton;
	}

	/**
	 * This method initializes jButton1	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButton1() {
		if (jButton1 == null) {
			jButton1 = new JButton();
			jButton1.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
			jButton1.setFont(new java.awt.Font("Microsoft Sans Serif", java.awt.Font.PLAIN, 11));
			jButton1.setBounds(new java.awt.Rectangle(497,418,89,21));
			jButton1.setBackground(java.awt.SystemColor.control);
			jButton1.setText("Cancel");
			jButton1.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					dispose();
					/*setEnabled(true);
					show();*/
					//setFocusable(true);
					
					
				}
			});
		
		}
		return jButton1;
	}

	@SuppressWarnings("unchecked")
	private void getEditorValues(){
		
		DefaultTableModel model = (DefaultTableModel)jTable.getModel();

		InstanceManager.eINSTANCE.updateInstances(model.getDataVector());
		Logger.log(Message.EDIT_FILE);

		
	}
}  //  @jve:decl-index=0:visual-constraint="10,10"
